﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Intorise.LLPM.Model;
using System.Security.Cryptography;
using System.Data;

namespace Intorise.LLPM.Model
{
    public partial class User
    {
        static Database db = new Database();

        public static User Login(string username, string password)
        {
            MD5 md5 = MD5.Create();
            var data = Encoding.Default.GetBytes(password);
            password = Encoding.Default.GetString(md5.ComputeHash(data, 0, data.Length));
            return db.User.Where(x => x.Account == username && x.Password == password).FirstOrDefault();
        }

        public static User Register(string username, string password)
        {
            MD5 md5 = MD5.Create();
            var data = Encoding.Default.GetBytes(password);
            password = Encoding.Default.GetString(md5.ComputeHash(data, 0, data.Length));
            User user = new User { ID = Guid.NewGuid(), Account = username, Password = password };
            db.User.AddObject(user);
            db.SaveChanges();
            return user;
        }
    }
}
